package com.lc.qianrenshiye.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;

import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.conn.Conn;
import com.lc.qianrenshiye.fragments.FragmentAllowance;
import com.lc.qianrenshiye.fragments.FragmentLoan;
import com.zcx.helper.bound.BoundView;

/**
 * Created by Administrator on 2016/11/19.
 */
public class MyInComeActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.income_rg) private RadioGroup income_rg;

    private Fragment[] fragments;

    private FragmentLoan fragmentLoan;
    private FragmentAllowance fragmentAllowance;

    private int prePosition = 0;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income);

        setBackTrue();

        setTitleName(getString(R.string.center_my_income_text));

        setAllwanceRight("货款返现：7天", R.color.text_color, this);

        fragmentAllowance = new FragmentAllowance();
        fragmentLoan = new FragmentLoan();

        fragments = new Fragment[]{fragmentLoan, fragmentAllowance };

        getSupportFragmentManager().beginTransaction().add(R.id.income_container_fl, fragmentLoan).show(fragmentLoan).commit();

        income_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {


                switch (checkedId) {

                    case R.id.income_loan_rb:

                        index = 0;

                        Log.e("------", prePosition + "    " + index);

                        if (prePosition != index) {

                            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();

                            trx.hide(fragments[prePosition]);
                            if (!fragments[index].isAdded()) {
                                trx.add(R.id.income_container_fl, fragments[index]);
                            }
                            trx.show(fragments[index]).commit();

                            prePosition = index;


                        }

                        break;

                    case R.id.income_allowance_rb:

                        index = 1;

                        Log.e("------", prePosition + "    " + index);

                        if (prePosition != index) {

                            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();

                            trx.hide(fragments[prePosition]);
                            if (!fragments[index].isAdded()) {
                                trx.add(R.id.income_container_fl, fragments[index]);
                            }
                            trx.show(fragments[index]).commit();

                            prePosition = index;


                        }

                        break;


                }

            }
        });



    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.title_right_allowance_rl:

                Bundle bundle = new Bundle();

                bundle.putString("url", Conn.SERVICE + Conn.WEB_LEVEL + "3");
                bundle.putString("title", "下款方式");

                startActivity(new Intent(this, MyWebActivity.class).putExtras(bundle));

                break;

        }

    }
}
